home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / t3dlib_src_r43.lha / decode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-02  |  2.1 KB  |  84 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. /* modified from uudecode.c V 5.3 from Berkeley 1/22/85 for use with */
  19. /* T3DLIB by Rob Hounsell */
  20.  
  21. #include <stdio.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24.  
  25. /* single character decode */
  26. #define DEC(c)    (((c) - ' ') & 077)
  27.  
  28. /*
  29.  * copy from buffer to out, decoding as you go along.
  30.  */
  31. decode_data(out, buffer)
  32. FILE *out;
  33. char *buffer;
  34. {
  35.     char *bp;
  36.     int n, bytecount, len, char_index;
  37.  
  38.     bytecount = 0;
  39.  
  40.     char_index = 0;
  41.     len = (int)strlen(buffer);
  42.     while (len > 0) {
  43.         n = DEC(buffer[char_index]);
  44.         if (n <= 0)
  45.             return(n);
  46.       bytecount += n;
  47.  
  48.         bp = &buffer[char_index+1];
  49.         while (n > 0) {
  50.             outdec(bp, out, n);
  51.             len -= 4;
  52.             char_index += 4;
  53.             bp += 4;
  54.             n -= 3;
  55.         }
  56.         char_index++; /* to account for the size byte for this run of */
  57.         len--;        /* up to 45 chars.                              */
  58.     }
  59.   return(bytecount);
  60. }
  61.  
  62. /*
  63.  * output a group of 3 bytes (4 input characters).
  64.  * the input chars are pointed to by p, they are to
  65.  * be output to file f.  n is used to tell us not to
  66.  * output all of them at the end of the file.
  67.  */
  68. outdec(p, f, n)
  69. char *p;
  70. FILE *f;
  71. {
  72.     int c1, c2, c3;
  73.  
  74.     c1 = DEC(*p) << 2 | DEC(p[1]) >> 4;
  75.     c2 = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
  76.     c3 = DEC(p[2]) << 6 | DEC(p[3]);
  77.     if (n >= 1)
  78.         putc(c1, f);
  79.     if (n >= 2)
  80.         putc(c2, f);
  81.     if (n >= 3)
  82.         putc(c3, f);
  83. }
  84.